package com.link.base.base.approval.service;

import com.link.base.base.approval.dao.mybatis.mapper.ApprovalNodeMapper;
import com.link.base.base.approval.model.ApprovalNode;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import javax.annotation.Resource;

import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/8 14:37
 * @version 1.0
 */

@Service
public class ApprovalNodeServiceImpl extends BasicServiceImpl<ApprovalNode> implements ApprovalNodeService {

    @Resource
    private ApprovalNodeMapper approvalNodeMapper;

    @Override
    public BasicMapper<ApprovalNode> getBasicMapper() {
        return approvalNodeMapper;
    }

    /**
     * 查询审批流程的第n个节点
     * @param record
     * @return ApprovalNode
     * @throws Exception
     */
    @Override
    public ApprovalNode queryOneApprovalNode(ApprovalNode record) throws Exception {
        List<ApprovalNode> list = approvalNodeMapper.queryOneApprovalNode(record);
        if (null == list || list.isEmpty()) {
            return null;
        } else {
            if (list.size() == 1) {
                return list.get(0);
            } else {
                throw new ServiceException("APPROVAL-001",record.getSn());
            }
        }
    }

    /**
     * 根据审核设置Id，查询第一条审核节点
     * @param approvalId
     * @return ApprovalNode
     * @throws Exception
     */
    @Override
    public ApprovalNode queryFirstApprovalNode(Long approvalId) throws Exception {
        if (approvalId == null) {
            throw new ServiceException("approval" + "PUBLIC-007");
        }
        ApprovalNode example = new ApprovalNode();
        example.setApprovalId(approvalId);
        example.setSn("1");
        ApprovalNode record = queryOneApprovalNode(example);
        if (record == null) {
            throw new ServiceException("APPROVAL-002");
        }
        return record;
    }

}
